package com.sali.数据结构;

/**
 数字在升序数组中出现的次数
 */
public class JZ_53 {

    public int GetNumberOfK (int[] nums, int k) {
        // write code here
        int leftIndex = getRes(nums, k - 0.5);
        int rightIndex = getRes(nums, k + 0.5);
        return rightIndex - leftIndex;
    }

    private int getRes(int[] nums, double val) {
        int left = 0;
        int right = nums.length - 1;
        while ( left <= right ) {
            int mid = left + ( right - left ) / 2;
            if ( nums[mid] < val ) {
                left = mid + 1;
            } else if ( nums[mid] > val ) {
                right = mid - 1;
            }
        }
        return left;
    }

}
